package com.api.shopapi.service.impl;

import com.api.shopapi.entity.SBJSleepDuration;
import com.api.shopapi.entity.SBJSleepReport;
import com.api.shopapi.entity.dto.SBJSleepReportDTO;
import com.api.shopapi.mapper.SBJSleepDurationMapper;
import com.api.shopapi.mapper.SBJSleepReportMapper;
import com.api.shopapi.service.SBJSleepReportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("SBJSleepReportServiceImplOne")
public class SBJSleepReportServiceImpl implements SBJSleepReportService {

    @Autowired
    private SBJSleepReportMapper sbjSleepReportMapper;

    @Autowired
    private SBJSleepDurationMapper sbjSleepDurationMapper;

    @Override
    public int insert(SBJSleepReport sbjSleepReport) {
        return sbjSleepReportMapper.insert(sbjSleepReport);
    }

    @Override
    public PageInfo<SBJSleepReportDTO> getSleepReportPageByDeviceId(int pageIndex, int pageSize, String deviceId, String startTime, String endTime) {
        PageHelper.startPage(pageIndex,pageSize);
        List<SBJSleepReportDTO> sbjSleepReportDTOList = sbjSleepReportMapper.getSleepReportPageByDeviceId(deviceId, startTime, endTime);
        for (SBJSleepReportDTO sbjSleepReportDTO : sbjSleepReportDTOList) {
            List<SBJSleepDuration> sbjSleepDurationList = sbjSleepDurationMapper.getListBySleepReportCode(sbjSleepReportDTO.getSleepReportCode());
            sbjSleepReportDTO.setSbjSleepDurationList(sbjSleepDurationList);
        }
        return new PageInfo<>(sbjSleepReportDTOList);
    }
}
